package 每日一题;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 会玩的洋洋
 * https://leetcode.cn/problems/design-authentication-manager/
 */
public class _1797_设计一个验证系统 {

}

/**
 * 执行用时：51ms，内存消耗：42.7MB
 */
class AuthenticationManager {
    int timeToLive;
    Map<String, Integer> codeTime;
    public AuthenticationManager(int timeToLive) {
        this.timeToLive = timeToLive;
        this.codeTime = new HashMap<>();
    }

    public void generate(String tokenId, int currentTime) {
        codeTime.put(tokenId, currentTime + timeToLive);
    }

    public void renew(String tokenId, int currentTime) {
        if (codeTime.containsKey(tokenId)) {
            int preTime = codeTime.get(tokenId);
            // 验证码未过期
            if (preTime > currentTime) {
                codeTime.put(tokenId, currentTime + timeToLive);
            } else { // 若验证法过期
                codeTime.remove(tokenId);
            }
        }
    }

    public int countUnexpiredTokens(int currentTime) {
        int count = 0;
        for (Integer value : codeTime.values()) {
            if (value > currentTime) {
                count++;
            }
        }
        return count;
    }
}